import Opt from '@/components/dt/info/Opt.js'
import toolMgr from '@/biz/base/tool.js'
import ruleOpt from '@/biz/option/rule.js'

class InfoOpt extends Opt {
  
  constructor(info) {
    super(info)
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let explain = {
      title: '撤场协议说明',
      placeholder: '请输入撤场协议说明',
      key: 'isWithdrawalDealExplain',
      visible: this.info.isWithdrawalDeal == '0',
      input: {
        type: 'textarea',
        autosize: {
          minRows: 3
        }
      },
      rules: [
        ruleOpt.required('请输入撤场协议说明'),
        ruleOpt.max(200)
      ]
    }
    let unSettlementRoomInfo = {
      title: '撤场协议是否包含未结算房源明细',
      placeholder: '请选择撤场协议是否包含未结算房源明细',
      key: 'unSettlementRoomInfo',
      visible: this.info.isWithdrawalDeal == '1',
      select: {
        options: [
          { key: '1', value: '是' },
          { key: '0', value: '否' },
        ]
      },
      rules: [
        ruleOpt.required('请选择撤场协议是否包含未结算房源明细'),
      ]
    }
    
    let files = []
    if (this.info.attachList && this.info.attachList.length > 0) {
      this.info.attachList.forEach(item => {
        if (item) {
          files.push({
            name: item.imageName,
            url: item.imageURL
          })
        }
      })
    }
    let accessory = {
      title: '撤场协议附件',
      key: 'attachList',
      visible: this.info.isWithdrawalDeal == '1',
      upload: {
        title: '上传文件',
        api: toolMgr.unloadFile(),
        files,
        multiple: true,
      },
      data: {
        to: (v) => {
          let attachList = v.map(item => {
            return {
              imageName: item.name,
              imageURL: item.response ? item.response.data.imageURL : item.url
            }
          })
          return {
            attachList
          }
        }
      },
      rules: [
        ruleOpt.required('请上传附件', 'array')
      ]
    }
    
    this.all = [{
        title: '撤场日期',
        placeholder: '请选择撤场日期',
        key: 'realityWithdrawalDate',
        date: {},
        rules: [
          ruleOpt.required('请选择撤场日期')
        ]
      },
      {
        title: '撤场协议是否签署',
        placeholder: '请选择撤场协议是否签署',
        key: 'isWithdrawalDeal',
        select: {
          options: [
            { key: '1', value: '是' },
            { key: '0', value: '否' },
          ]
        },
        change: ({ info }) => {
          if (info.isWithdrawalDeal == '1') {
            unSettlementRoomInfo.visible = true
            accessory.visible = true
            explain.visible = false
          } else {
            unSettlementRoomInfo.visible = false
            accessory.visible = false
            explain.visible = true
          }
        },
        rules: [
          ruleOpt.required('请选择撤场协议是否签署'),
        ]
      },
      unSettlementRoomInfo,
      accessory,
      explain,
    ]
  }
}

export default InfoOpt
